package com.jjshop.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.io.File;

public class ShareUtils {
    public static void sendVideo(Context context, String extraText, File file) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            Intent intent = new Intent(Intent.ACTION_SEND);

            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, "com.jjdance.fileProvider", file);
            //            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

            intent.setType("video/*");
            intent.putExtra(Intent.EXTRA_SUBJECT, extraText);
            //            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.putExtra(Intent.EXTRA_STREAM, contentUri);
            intent.putExtra(Intent.EXTRA_TEXT, extraText);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(Intent.createChooser(intent, "请选择要发送的目标："));

        }else{
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("video/*");
            intent.putExtra(Intent.EXTRA_SUBJECT, extraText);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.putExtra(Intent.EXTRA_TEXT, extraText);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(Intent.createChooser(intent, "请选择要发送的目标："));

        }


    }

    public static void sendAudio(Context context, String extraText, File file) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            Intent intent = new Intent(Intent.ACTION_SEND);

            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, "com.jjdance.fileProvider", file);
            //            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

            intent.setType("audio/*");
            intent.putExtra(Intent.EXTRA_SUBJECT, extraText);
            //            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.putExtra(Intent.EXTRA_STREAM, contentUri);
            intent.putExtra(Intent.EXTRA_TEXT, extraText);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(Intent.createChooser(intent, "请选择要发送的目标："));
        }else{
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("audio/*");
            intent.putExtra(Intent.EXTRA_SUBJECT, extraText);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.putExtra(Intent.EXTRA_TEXT, extraText);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(Intent.createChooser(intent, "请选择要发送的目标："));

        }


    }

    public static void sendText(Context context, String text) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, "分享99广场舞"));
    }


    public static void shareWeb(final Activity context, final String title, final String content, final String img, final String url, final UMShareListener listener) {
        //        final UMShareListener mShareListener = new CustomShareListener(context);
        ShareAction mShareAction = new ShareAction(context)
                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        UMWeb web = new UMWeb(url);
                        web.setTitle(title);
                        web.setDescription(content);
                        web.setThumb(new UMImage(context, img));
                        new ShareAction(context).withMedia(web)
                                .setPlatform(share_media)
                                .setCallback(listener)
                                .share();
                    }
                });
        mShareAction.open();

    }
    public interface OnJJMailCallBack{
        void onCopy();
        void onCallBack();
        void onSave();
        void onMoreImg();
    }
    public static void shareJJmailProductShop(final Activity context, final String title, final String content,
                                              final String img, final String url, final String shareImgs,
                                              boolean isHaveImg,
                                              final OnJJMailCallBack onJJMailCallBack,
                                              final UMShareListener listener){
        ShareAction mShareAction = new ShareAction( context)
                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        if (isHaveImg){
            mShareAction.addButton("二维码","save_img","save_img","");
            mShareAction.addButton("复制图片","share_img","share_img","");
        }

        mShareAction.addButton("复制链接","share_url","share_url","")
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        if (share_media == null){
                            if ("share_url".equals(snsPlatform.mKeyword)){
                                if (onJJMailCallBack != null){
                                    onJJMailCallBack.onCopy();
                                }
                            }else if ("share_img".equals(snsPlatform.mKeyword)){
                                if (onJJMailCallBack != null){
                                    onJJMailCallBack.onCallBack();
                                }
                            }else if ("save_img".equals(snsPlatform.mKeyword)){
                                if (onJJMailCallBack != null){
                                    onJJMailCallBack.onSave();
                                }
                            }
                        }else{
                            if (!TextUtils.isEmpty(shareImgs)){
                                if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE){
                                    if (onJJMailCallBack != null){
                                        onJJMailCallBack.onMoreImg();
                                    }

                                }else{

                                    UMWeb web = new UMWeb(url);
                                    web.setTitle(content);
                                    web.setDescription(title);
                                    web.setThumb(new UMImage(context, img));
                                    new ShareAction(context).withMedia(web)
                                            .setPlatform(share_media)
                                            .setCallback(listener)
                                            .share();

                                }
                            }else {
                                UMWeb web = new UMWeb(url);
                                web.setTitle(content);
                                web.setDescription(title);
                                web.setThumb(new UMImage(context, img));
                                new ShareAction(context).withMedia(web)
                                        .setPlatform(SHARE_MEDIA.QQ)
                                        .setCallback(listener)
                                        .share();
                            }
                        }
                    }
                });
        ShareBoardConfig config = new ShareBoardConfig();//新建ShareBoardConfig
        //        config.setShareboardPostion(ShareBoardConfig.SHAREBOARD_POSITION_CENTER);//设置位置
        //        config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_CIRCULAR);
        //        config.setCancelButtonVisibility(true);
        config.setIndicatorVisibility(false);
        mShareAction.open(config);
        //        mShareAction.open();
    }

    public static void shareJJmail(final Activity context, final String title, final String content, final String img, final String url, final UMShareListener listener){
        ShareAction mShareAction = new ShareAction( context)
                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .addButton("share_url","share_url","share_url","")
                //                .addButton("share_img","share_img","share_img","")
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        if (share_media == null){
                            if ("share_url".equals(snsPlatform.mKeyword)){
                                UIUtils.showToast("1");
                            }else if ("share_img".equals(snsPlatform.mKeyword)){
                                UIUtils.showToast("2");
                            }
                        }else{
                            UMWeb web = new UMWeb(url);
                            web.setTitle(title);
                            web.setDescription(content);
                            web.setThumb(new UMImage(context, img));
                            new ShareAction(context).withMedia(web)
                                    .setPlatform(share_media)
                                    .setCallback(listener)
                                    .share();
                        }
                    }
                });
        mShareAction.open();
    }

    public static int getType(SHARE_MEDIA share_media) {
        if (share_media == SHARE_MEDIA.WEIXIN){
            return 1;
        }else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE){
            return 2;
        }else if (share_media == SHARE_MEDIA.QQ){
            return 3;
        }else if(share_media == SHARE_MEDIA.QZONE){
            return 4;
        }else{
            return 0;
        }
    }
}